{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 4.11 \u540c\u65f6\u8fed\u4ee3\u591a\u4e2a\u5e8f\u5217\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u540c\u65f6\u8fed\u4ee3\u591a\u4e2a\u5e8f\u5217\uff0c\u6bcf\u6b21\u5206\u522b\u4ece\u4e00\u4e2a\u5e8f\u5217\u4e2d\u53d6\u4e00\u4e2a\u5143\u7d20\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e3a\u4e86\u540c\u65f6\u8fed\u4ee3\u591a\u4e2a\u5e8f\u5217\uff0c\u4f7f\u7528 zip() \u51fd\u6570\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "xpts = [1, 5, 4, 2, 10, 7]\nypts = [101, 78, 37, 15, 62, 99]\nfor x, y in zip(xpts, ypts):\n    print(x,y)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "zip(a, b) \u4f1a\u751f\u6210\u4e00\u4e2a\u53ef\u8fd4\u56de\u5143\u7ec4 (x, y) \u7684\u8fed\u4ee3\u5668\uff0c\u5176\u4e2dx\u6765\u81eaa\uff0cy\u6765\u81eab\u3002\n\u4e00\u65e6\u5176\u4e2d\u67d0\u4e2a\u5e8f\u5217\u5230\u5e95\u7ed3\u5c3e\uff0c\u8fed\u4ee3\u5ba3\u544a\u7ed3\u675f\u3002\n\u56e0\u6b64\u8fed\u4ee3\u957f\u5ea6\u8ddf\u53c2\u6570\u4e2d\u6700\u77ed\u5e8f\u5217\u957f\u5ea6\u4e00\u81f4\u3002"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "a = [1, 2, 3]\nb = ['w', 'x', 'y', 'z']\nfor i in zip(a,b):\n    print(i)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u8fd9\u4e2a\u4e0d\u662f\u4f60\u60f3\u8981\u7684\u6548\u679c\uff0c\u90a3\u4e48\u8fd8\u53ef\u4ee5\u4f7f\u7528 itertools.zip_longest() \u51fd\u6570\u6765\u4ee3\u66ff\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from itertools import zip_longest\nfor i in zip_longest(a,b):\n    print(i)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "for i in zip_longest(a, b, fillvalue=0):\n    print(i)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5f53\u4f60\u60f3\u6210\u5bf9\u5904\u7406\u6570\u636e\u7684\u65f6\u5019 zip() \u51fd\u6570\u662f\u5f88\u6709\u7528\u7684\u3002\n\u6bd4\u5982\uff0c\u5047\u8bbe\u4f60\u5934\u5217\u8868\u548c\u4e00\u4e2a\u503c\u5217\u8868\uff0c\u5c31\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "headers = ['name', 'shares', 'price']\nvalues = ['ACME', 100, 490.1]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f7f\u7528zip()\u53ef\u4ee5\u8ba9\u4f60\u5c06\u5b83\u4eec\u6253\u5305\u5e76\u751f\u6210\u4e00\u4e2a\u5b57\u5178\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "s = dict(zip(headers,values))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6216\u8005\u4f60\u4e5f\u53ef\u4ee5\u50cf\u4e0b\u9762\u8fd9\u6837\u4ea7\u751f\u8f93\u51fa\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "for name, val in zip(headers, values):\n    print(name, '=', val)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u867d\u7136\u4e0d\u5e38\u89c1\uff0c\u4f46\u662f zip() \u53ef\u4ee5\u63a5\u53d7\u591a\u4e8e\u4e24\u4e2a\u7684\u5e8f\u5217\u7684\u53c2\u6570\u3002\n\u8fd9\u65f6\u5019\u6240\u751f\u6210\u7684\u7ed3\u679c\u5143\u7ec4\u4e2d\u5143\u7d20\u4e2a\u6570\u8ddf\u8f93\u5165\u5e8f\u5217\u4e2a\u6570\u4e00\u6837\u3002\u6bd4\u5982;"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "a = [1, 2, 3]\nb = [10, 11, 12]\nc = ['x','y','z']\nfor i in zip(a, b, c):\n    print(i)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6700\u540e\u5f3a\u8c03\u4e00\u70b9\u5c31\u662f\uff0c zip() \u4f1a\u521b\u5efa\u4e00\u4e2a\u8fed\u4ee3\u5668\u6765\u4f5c\u4e3a\u7ed3\u679c\u8fd4\u56de\u3002\n\u5982\u679c\u4f60\u9700\u8981\u5c06\u7ed3\u5bf9\u7684\u503c\u5b58\u50a8\u5728\u5217\u8868\u4e2d\uff0c\u8981\u4f7f\u7528 list() \u51fd\u6570\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "zip(a, b)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "list(zip(a, b))"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}